package com.xdtech.parking.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.parking.model.ParkingDevice;
import com.xdtech.parking.service.ParkingDeviceService;
import com.xdtech.parking.vo.ParkingDeviceItem;
import com.xdtech.sys.aspect.SystemControllerLog;
import com.xdtech.web.model.ComboBox;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-06-18 14:30:33
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class ParkingDeviceController {
	private Log log = LogFactory.getLog(ParkingDeviceController.class);
	@Autowired
	private ParkingDeviceService parkingDeviceService;
	@RequestMapping(value="/parkingDevice.do",params = "parkingDevice")
	public ModelAndView skipParkingDevice() {
		return new ModelAndView("parking/parkingDevice/parkingDevice_ftl");
	}
	
	@RequestMapping(value="/parkingDevice.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(ParkingDeviceItem item,Pagination pg) {
		Map<String, Object> results =  parkingDeviceService.loadPageDataByConditions(pg,item,"findParkingDeviceByCondition");
		return results;
	}
	
	@RequestMapping(value="/parkingDevice.do",params = "editParkingDevice")
	@SystemControllerLog(description = "编辑")
	public ModelAndView editParkingDevice(HttpServletRequest request,Long parkingDeviceId) {
		if (parkingDeviceId!=null) {
			request.setAttribute("parkingDeviceItem", parkingDeviceService.loadParkingDeviceItem(parkingDeviceId));
		}
		return new ModelAndView("parking/parkingDevice/editParkingDevice_ftl");
	}
	
	@RequestMapping(value="/parkingDevice.do",params = "addParkingDevice")
	@SystemControllerLog(description = "新增")
	public ModelAndView addParkingDevice(HttpServletRequest request) {
		return new ModelAndView("parking/parkingDevice/addParkingDevice_ftl");
	}
	
	
	@RequestMapping(value="/parkingDevice.do",params = "saveParkingDevice")
	@ResponseBody
	@SystemControllerLog(description = "保存")
	public ResultMessage saveParkingDevice(ParkingDeviceItem item) {
		ResultMessage r = new ResultMessage();
		item.setUkey(item.getAreaCode()+"_"+item.getDeviceCode());
		if (item.getId()==null) {
			//设置默认值
//			item.setHasCar(hasCar)
			//初始化状态
			item.setStatus(0);
			//初始化状态
			item.setSetVal("0");
		}
		if (parkingDeviceService.saveOrUpdateParkingDevice(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/parkingDevice.do",params = "deleteParkingDeviceItems")
	@ResponseBody
	@SystemControllerLog(description = "删除")
	public ResultMessage deleteParkingDeviceItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> parkingDeviceIds = new ArrayList<Long>();
			for (String id : tempIds) {
				parkingDeviceIds.add(Long.valueOf(id));
			}
			parkingDeviceService.deleteParkingDeviceInfo(parkingDeviceIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	/**
	 * 验证配置是否重复
	 * 
	 * @author max.zheng
	 * @create 2015-4-5下午10:12:54
	 * @modified by
	 * @param id
	 * @param roomType
	 * @return
	 */
	@RequestMapping(value="/parkingDevice.do",params = "checkDeviceInAreaIsExit")
	@ResponseBody
	public boolean checkDeviceInAreaIsExit(String areaCode,String deviceCode) {
		if (StringUtils.isBlank(areaCode)||StringUtils.isBlank(deviceCode))
		{
			return false;
		}else
		{
			boolean rs = parkingDeviceService.checkDeviceCodeIsExit(areaCode,deviceCode);
			return !rs;
		}
		
	}

	@RequestMapping(value="/parkingDevice.do",params = "loadDevices")
	@ResponseBody
	public List<ComboBox> loadDevices(String value,String text) {
		List<ParkingDevice> devices = parkingDeviceService.getAll();
		List<ComboBox> comboBoxs = new ArrayList<ComboBox>();
		for (ParkingDevice dv : devices)
		{
			comboBoxs.add(new ComboBox(dv.getId().toString(), dv.getUkey(), dv.getId().toString().equals(value)||dv.getUkey().equals(text)));
		}
		return comboBoxs;
	}
}
